package io.renren.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.annotation.LoginUser;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.dao.FoodDao;
import io.renren.dao.StockOrdermxDao;
import io.renren.dto.FoodDTO;
import io.renren.dto.MarketmxDTO;
import io.renren.entity.FoodEntity;
import io.renren.entity.UserEntity;
import io.renren.service.AuxiliaryMateriService;
import io.renren.service.FoodService;
import io.renren.service.StockOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 食材表（辅材共用）
 *
 * @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class FoodServiceImpl extends CrudServiceImpl<FoodDao, FoodEntity, FoodDTO> implements FoodService {
    @Autowired
    private FoodDao foodDao;

    public QueryWrapper<FoodEntity> getWrapper(Map<String, Object> params){
        String type=(String)params.get("type");
        String variety=(String)params.get("variety");
        String foodName=(String)params.get("foodName");
        QueryWrapper<FoodEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(type), "type", type);
        wrapper.eq(StringUtils.isNotBlank(variety), "variety", variety);
        wrapper.like(StringUtils.isNotBlank(foodName),"food_name",foodName);
        return wrapper;
    }

    @Override
    public FoodDTO get(long id){
        FoodEntity  foodEntity =baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(foodEntity, FoodDTO.class);
    }


    @Override
    public List<FoodDTO> supplierFoodId( Long id,String variety,String name,String type,int sign,Long orderId, @LoginUser UserEntity entity) {
        System.out.println(id +"翟"+variety+"文"+name+"豆腐"+type+"冬瓜"+sign+"黄瓜"+entity);
        String supplierid ="";
        String foodName ="";
        String variety1 ="";
        String type1 ="";
        if (StringUtils.isNotBlank(name)) {
            foodName ="and cf.food_name LIKE  '%"+name+"%' ";
        }
        if (null !=id) {
            supplierid ="and SF.supplier_id  = "+id;
        }
        if (StringUtils.isNotBlank(variety)) {
            variety1 ="and CF.variety  = "+variety;
        }
        if (StringUtils.isNotBlank(type)) {
            type1 ="and CF.type  = "+type;
        }
        List<FoodDTO>  foodDTOS= foodDao.supplierFoodId(supplierid,variety1, foodName,type1);
        for (FoodDTO foodDTO :foodDTOS) {
            if(null != orderId){
                FoodDTO temFood;
                if(0 == sign){
                    temFood = foodDao.gysOrdermx(orderId,foodDTO.getFoodId());
                }else if(1 == sign){
                    temFood = foodDao.gysAuxiliarymx(orderId,foodDTO.getFoodId());
                }else if(2 == sign){
                    temFood = foodDao.gyssmallmx(orderId,foodDTO.getFoodId());
                }else {
                    temFood = foodDao.gyscollarmx(orderId,foodDTO.getFoodId(),entity.getDeptId());
                }
                if (null != temFood) {
                    foodDTO.setFlag(1);
                    foodDTO.setPurchaseNum(temFood.getPurchaseNum());
                }
            }else {
                FoodDTO temFood = foodDao.getTemFood(foodDTO.getFoodId(), foodDTO.getSupplierId(), sign, entity.getId(), entity.getDeptId());
                if (null != temFood) {
                    foodDTO.setFlag(1);
                    foodDTO.setPurchaseNum(temFood.getPurchaseNum());
                }
            }
        }
        return foodDTOS;
    }

    /**
     * 根据模糊查询获取当前的所有菜品，当前菜品的名称的供应商，和临时表中已经被选中的菜，根据类 1型食材、2辅材、3大食堂
     * @param foodName
     * @return
     */
    @Override
    public Map getSupplierName(String foodName,int sign,Long orderId, @LoginUser UserEntity entity) {
        System.out.println(foodName+"模糊查询" +sign);
        List<Map> list = new ArrayList<>();
        Map map=new HashMap();
        Map map1 = new HashMap();
        if(3!=sign) {
            List<FoodDTO> suprNameList;//供应商查询
            String types;
            if (1 == sign) {//辅材
                types = " and A.type = 2";
                suprNameList = foodDao.getSupplierName(foodName, types);
            } else {
                types = " and A.type = 1";
                suprNameList = foodDao.getSupplierName(foodName, types);
            }
            if (null == suprNameList) {
                return map;
            }
            for (FoodDTO foodDTO : suprNameList) {
                map1 = new HashMap();
                map1.put("supplierName", foodDTO.getSupplierName());
                map1.put("supplierId", foodDTO.getSupplierId());
                List<FoodDTO> supplierFoodList = foodDao.getSupplierFood(foodName, foodDTO.getSupplierId());//供应商下面所有的食材
                for (FoodDTO foodDTO1 : supplierFoodList) {
                    FoodDTO temFood;
                    if (null == orderId) {
                        temFood = foodDao.getTemFood(foodDTO1.getFoodId(), foodDTO.getSupplierId(), sign, entity.getId(), entity.getDeptId());
                    } else if (0 == sign) {
                        temFood = foodDao.gysOrdermx(orderId, foodDTO1.getFoodId());
                    } else if (1 == sign) {
                        temFood = foodDao.gysAuxiliarymx(orderId, foodDTO1.getFoodId());
                    } else {
                        temFood = foodDao.gyssmallmx(orderId, foodDTO1.getFoodId());
                    }
                    if (null != temFood) {
                        foodDTO1.setFlag(1);
                        foodDTO1.setPurchaseNum(temFood.getPurchaseNum());
                    }
                }
                map1.put("foodDTO", supplierFoodList);
                list.add(map1);
            }
            map.put("list",list);
        }else {
            map1.put("supplierName", "辅材领取列表");
            map1.put("supplierId", entity.getDeptId());
            List<FoodDTO> temFood;
            temFood = foodDao.getFoodmx(foodName,entity.getDeptId());
            if(null ==temFood){
                return map;
            }
            for (FoodDTO foodDTO:temFood) {
                foodDTO.setSupplierId(entity.getDeptId());
                FoodDTO temFood1;
                if(null ==orderId){
                     temFood1 = foodDao.getcollFood(foodDTO.getFoodId(), sign, entity.getId(), entity.getDeptId());
                }else {
                     temFood1 = foodDao.gyscollarmx(orderId, foodDTO.getFoodId(),entity.getDeptId());
                }
                if(null!=temFood1){
                    foodDTO.setFlag(1);
                    foodDTO.setPurchaseNum(temFood1.getPurchaseNum());
                }
            }
            map1.put("foodDTO", temFood);
            list.add(map1);
            map.put("list",list);
        }
        return map;
    }

    /**
     * 辅材详情表查询
     * @param variety
     * @param name
     * @return
     */
    @Override
    public List<FoodDTO> selectCoolerFoodId( String name,String variety,int sign,Long orderId, @LoginUser UserEntity entity) {

        String foodName1 ="";
        String variety1 ="";
        if (null!=name) {
            foodName1 ="and fd.food_name LIKE  '%"+name+"%' ";
        }
        if (null != variety ) {
            variety1 ="and fd.variety  = "+variety;
        }
        List<FoodDTO>  list= foodDao.selectCoolerFoodId(variety1,foodName1);
        for (FoodDTO foodDTO:list) {
            FoodDTO temFood = foodDao.getTemCollFood(foodDTO.getFoodId(), sign,orderId,entity.getDeptId() );
            if(null != temFood){
                foodDTO.setFlag(1);
                foodDTO.setPurchaseNum(temFood.getPurchaseNum());
            }
        }
        return list;
    }

}